package com.seleniti.enpareja.web.desktop.client.control.factory;

import org.ioc.commons.flowcontrol.messageboxfactory.MessageBoxFactory.ConfirmResponse;
import org.ioc.commons.impl.gwt.client.flowcontrol.placecontroller.DelegateAdapter;
import org.ioc.commons.impl.gwt.client.flowcontrol.placecontroller.PlaceHasData;

import com.seleniti.enpareja.controller.control.factory.ClientFactory;

public class EnParejaDelegateAdapter extends DelegateAdapter {
	/**
	 * 
	 */
	private final ClientFactory clientFactory;

	private PlaceHasData nextPlace;
	private boolean confirmResponse;

	/**
	 * @param clientFactory
	 */
	public EnParejaDelegateAdapter(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
	}

	@Override
	public boolean confirm(String message) {
		if (!confirmResponse) {
			clientFactory.getMessageBoxFactory().forThisObject(this).confirm("Pending changes detected", message, new ConfirmResponse() {

				@Override
				public void onUserResponse(boolean userResponse) {
					confirmResponse = userResponse;
					if (userResponse && nextPlace != null) {
						clientFactory.getPlaceController().forThisObject(EnParejaDelegateAdapter.this).goTo(nextPlace);
					}

				}
			});

			return false;
		} else {
			confirmResponse = false;
			return true;
		}
	}
}